﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Utils;
using System.Threading;
using System.Threading.Tasks;
using DevExpress.XtraBars;

namespace ServiceCenterApp
{
    public partial class MainWindow : DevExpress.XtraEditors.XtraForm
    {
        public MainWindow()
        {
            InitializeComponent();
            Form.CheckForIllegalCrossThreadCalls = false;
        }

        public void StartProgress()
        {
            this.ProgressBar.EditValue = 0;
            this.ProgressBar.Enabled = true;
            Task task = new Task(new Action(() =>
            {
                int value = 0;
                while (this.ProgressBar.Enabled)
                {
                    value = (int)this.ProgressBar.EditValue;
                    if (value >= this.repositoryItemProgressBar1.Maximum)
                    {
                        value = 0;
                    }
                    else
                    {
                        value++;
                    }
                    this.ProgressBar.EditValue = value;
                    Thread.Sleep(100);
                    Application.DoEvents();
                }
            }));
            task.Start();
        }

        public void StopProgress()
        {
            this.ProgressBar.EditValue = 0;
            this.ProgressBar.Enabled = false;
        }

        public void ShowMessage(string text)
        {
            this.barMessage.Caption = text;
            //this.Update();
        }

        public void ShowAddProductForm()
        {
            barButtonItem7_ItemClick(null, null);
        }

        private void MainWindow_Load(object sender, EventArgs e)
        {
            barButtonItem14_ItemClick(null, null);
        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var form = this.MdiChildren.Where(x => x is FormCateogory).FirstOrDefault();
            if (form != null)
            {
                form.Activate();
                form.BringToFront();
            }
            else
            {
                form = new FormCateogory();
                form.MdiParent = this;
                form.Show();
            }
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.ActiveMdiChild != null && this.ActiveMdiChild is IChildForm)
            {
                backgroundWork(() => { ((IChildForm)ActiveMdiChild).Save(); });
            }
        }

        private void barButtonItem17_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.MdiChildren.ToList().ForEach(x =>
            {
                if (x is IChildForm)
                {
                    ((IChildForm)x).Save();
                }
            });
        }

        private void barButtonItem18_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.ActiveMdiChild != null && this.ActiveMdiChild is IChildForm)
            {
                backgroundWork(() => { ((IChildForm)ActiveMdiChild).Delete(); });
                //((IChildForm)ActiveMdiChild).Delete(); 
            }
        }

        private void barButtonItem19_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.ActiveMdiChild != null && this.ActiveMdiChild is IChildForm)
            {
                backgroundWork(() => { ((IChildForm)ActiveMdiChild).RefreshData(); });
            }
        }

        private void barButtonItem20_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.ActiveMdiChild != null && this.ActiveMdiChild is IChildForm)
            {
                ((IChildForm)ActiveMdiChild).Close();
            }
        }

        private void barButtonItem5_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var form = this.MdiChildren.Where(x => x is FormKeys).FirstOrDefault();
            if (form != null)
            {
                form.Activate();
                form.BringToFront();
            }
            else
            {
                form = new FormKeys();
                form.MdiParent = this;
                form.Show();
            }
        }
        
        private void barButtonItem10_ItemClick(object sender, ItemClickEventArgs e)
        {
            var form = this.MdiChildren.Where(x => x is FormEntity).FirstOrDefault();
            if (form != null)
            {
                form.Activate();
                form.BringToFront();
            }
            else
            {
                form = new FormEntity();
                form.MdiParent = this;
                form.Show();
            }
        }
     
        void backgroundWork(Action action)
        {
            BackgroundWorker worker = new BackgroundWorker();
            worker.RunWorkerCompleted += worker_RunWorkerCompleted;
            worker.DoWork += worker_DoWork;
            worker.RunWorkerAsync(action);
            StartProgress();
        }

        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            StopProgress();
        }

        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            Action action = e.Argument as Action;
            action();
        }

        private void barButtonItem8_ItemClick(object sender, ItemClickEventArgs e)
        {
            var form = this.MdiChildren.Where(x => x is FormProductList).FirstOrDefault();
            if (form != null)
            {
                form.Activate();
                form.BringToFront();
            }
            else
            {
                form = new FormProductList();
                form.MdiParent = this;
                form.Show();
            }
        }

        private void barButtonItem14_ItemClick(object sender, ItemClickEventArgs e)
        {
            var form = this.MdiChildren.Where(x => x is FormSearch).FirstOrDefault();
            if (form != null)
            {
                form.Activate();
                form.BringToFront();
            }
            else
            {
                form = new FormSearch();
                form.MdiParent = this;
                form.Show();
            }
        }

        private void barButtonItem6_ItemClick(object sender, ItemClickEventArgs e)
        {
            barButtonItem14_ItemClick(null, null);
        }

        private void btnBack_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (this.ActiveMdiChild != null && this.ActiveMdiChild is INavigateForm)
            {
                backgroundWork(() => { ((INavigateForm)ActiveMdiChild).Back(); });
            }
        }

        private void btnForward_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (this.ActiveMdiChild != null && this.ActiveMdiChild is INavigateForm)
            {
                backgroundWork(() => { ((INavigateForm)ActiveMdiChild).Forward(); });
            }
        }

        private void barButtonItem7_ItemClick(object sender, ItemClickEventArgs e)
        {
            var form = this.MdiChildren.Where(x => x is FormAddProduct).FirstOrDefault();
            if (form != null)
            {
                form.Activate();
                form.BringToFront();
            }
            else
            {
                form = new FormAddProduct();
                form.MdiParent = this;
                form.Show();
            }
        }
    }
}